package com.xxx.meter.ui.info;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.google.zxing.client.android.Intents;
import com.xxx.meter.R;
import com.xxx.meter.base.BaseActivity;
import com.xxx.meter.model.http.Api;
import com.xxx.meter.model.http.ApiCallback;
import com.xxx.meter.model.http.bean.MachineChildBean;
import com.xxx.meter.model.utils.ButtonUtil;
import com.xxx.meter.model.utils.PermissionUtil;
import com.xxx.meter.model.utils.ToastUtil;
import com.xxx.meter.ui.info.param.InfoMachineFindIndexActivity;
import com.xxx.meter.ui.info.param.InfoMachineRecordActivity;
import com.xxx.meter.ui.info.web.InfoFindWebActivity;
import com.xxx.meter.ui.main.SweepActivity;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;

public class InfoDeviceActivity extends BaseActivity {

    @BindView(R.id.main_title)
    TextView mTitle;

    private int type;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_info;
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        type = intent.getIntExtra("type", 0);
        mTitle.setText(title);
    }

    @OnClick({R.id.main_return, R.id.device_info_sweep_find, R.id.device_info_index_find})
    public void OnClick(View view) {
        if (ButtonUtil.isFastClick()) return;
        switch (view.getId()) {
            case R.id.main_return:
                finish();
                break;
            case R.id.device_info_sweep_find:
                if (PermissionUtil.checkPermission(this, PermissionUtil.READ_PERMISSION, PermissionUtil.WRITE_PERMISSION, PermissionUtil.CAMERA_PERMISSION)) {
                    startActivityForResult(new Intent(this, SweepActivity.class), 1);
                }
                break;
            case R.id.device_info_index_find:
                Intent intent = new Intent(this, InfoMachineFindIndexActivity.class);
                intent.putExtra("type", type);
                startActivity(intent);
                break;
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean result = PermissionUtil.onRequestPermissionsResult(permissions, grantResults);
        if (result) {
            startActivityForResult(new Intent(this, SweepActivity.class), 1);
        } else {
            if (PermissionUtil.checkPermission(this, PermissionUtil.READ_PERMISSION, PermissionUtil.WRITE_PERMISSION, PermissionUtil.CAMERA_PERMISSION)) {
                startActivityForResult(new Intent(this, SweepActivity.class), 1);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //扫码返回的
        if (resultCode == Activity.RESULT_OK && data != null) {
            String content = data.getStringExtra(Intents.Scan.RESULT);
            loadSweepDevice(content);
        }
    }

    /**
     * @Model 加载扫描设备
     */
    private void loadSweepDevice(String sweepCode) {
        Api.getInstance().sweepDevice(sweepCode, "1")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ApiCallback<MachineChildBean>(this, false) {

                    @Override
                    public void onSuccess(MachineChildBean bean) {
                        Intent intent = null;
                        switch (type) {
                            case 1:
                                intent = new Intent(InfoDeviceActivity.this, InfoFindWebActivity.class);
                                intent.putExtra("machineId", bean.getId());
                                intent.putExtra("machineType", bean.getType());
                                intent.putExtra("title", bean.getName());
                                intent.putExtra("type", 1);
                                startActivity(intent);
                                break;
                            case 0:
                                intent = new Intent(InfoDeviceActivity.this, InfoMachineRecordActivity.class);
                                intent.putExtra("machineId", bean.getId());
                                intent.putExtra("machineType", bean.getType());
                                intent.putExtra("machineName", bean.getName());
                                break;
                        }
                        if (intent != null) {
                            startActivity(intent);
                        }
                    }

                    @Override
                    public void onError(int errorCode, String errorMessage) {
                        ToastUtil.showToast(errorMessage);
                    }
                });
    }


}
